/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 29.07.2005
 *
 *************************************************/
package fightplan.views.tables;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;

import fightplan.bindx.GObservableList;
import fightplan.business.Category;
import fightplan.core.Style;
import fightplan.ui.BeanListTableModel;
import fightplan.ui.BeanPropertyTableCellRenderer;

@SuppressWarnings("serial")
public class CategoryPreviewTable extends JPanel {

	public CategoryPreviewTable(GObservableList<Category> categories) {
		super(new BorderLayout());

		BeanListTableModel<Category> tableModel = new BeanListTableModel<Category>(categories, new String[] {
				"categoryType", "gender", "yearRangeFormatted", "weightRangeFormatted", "beltRangeFormatted" },
				new String[] { "Art", "D/H", "Jahrgang", "Gewicht", "Gurt", });
		tableModel.setColumnReadOnly(0);
		tableModel.setColumnReadOnly(1);
		tableModel.setColumnReadOnly(2);
		tableModel.setColumnReadOnly(3);
		tableModel.setColumnReadOnly(4);

		JTable table = new JTable(tableModel);
		table.setRowHeight(20);

		TableColumnModel cm = table.getColumnModel();
		cm.getColumn(0).setCellRenderer(new BeanPropertyTableCellRenderer("name"));
		cm.getColumn(1).setCellRenderer(new BeanPropertyTableCellRenderer("nameLong"));

		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setPreferredSize(new Dimension(500, 180));
		scrollPane.getViewport().setBackground(Style.UNUSED_TABLE_SPACE);
		scrollPane.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				JTable table = (JTable) ((JScrollPane) e.getSource()).getViewport().getView();
				table.getSelectionModel().clearSelection();
				table.removeEditor();
			}
		});
		add(scrollPane, BorderLayout.CENTER);
	}

}